package com.xmtlxx.web.controller.oms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.web.controller.oms.service.IPropertyLogService;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.oms.domain.bo.PropertyLogBo;
import com.xmtlxx.oms.domain.vo.PropertyLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 财物日志Controller
 *
 * @author xmtlxx
 * @date 2022-11-28
 */
@Validated
@Api(value = "财物日志控制器", tags = {"财物日志管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/oms/propertyLog")
public class PropertyLogController extends BaseController {

    private final IPropertyLogService iPropertyLogService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<PropertyLogVo> initBean(){
        PropertyLogVo vo = new PropertyLogVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询财物日志列表
     */
    @ApiOperation("查询财物日志列表")
//    @SaCheckPermission("oms:propertyLog:list")
    @GetMapping("/list")
    public TableDataInfo<PropertyLogVo> list(@Validated(QueryGroup.class) PropertyLogBo bo, PageQuery pageQuery) {
        return iPropertyLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出财物日志列表
     */
    @ApiOperation("导出财物日志列表")
    @SaCheckPermission("oms:propertyLog:export")
    @Log(title = "财物日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated PropertyLogBo bo, HttpServletResponse response) {
        List<PropertyLogVo> list = iPropertyLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "财物日志", PropertyLogVo.class, response);
    }

    /**
     * 获取财物日志详细信息
     */
    @ApiOperation("获取财物日志详细信息")
//    @SaCheckPermission("oms:propertyLog:query")
    @GetMapping("/{id}")
    public R<PropertyLogVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iPropertyLogService.queryById(id));
    }

    /**
     * 新增财物日志
     */
    @ApiOperation("新增财物日志")
    @SaCheckPermission("oms:propertyLog:add")
    @Log(title = "财物日志", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PropertyLogVo vo) {
        PropertyLogBo bo = new PropertyLogBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iPropertyLogService.insertByBo(bo) != null ? 1 : 0);
    }

    /**
     * 修改财物日志
     */
    @ApiOperation("修改财物日志")
    @SaCheckPermission("oms:propertyLog:edit")
    @Log(title = "财物日志", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PropertyLogVo vo) {
        PropertyLogBo bo = new PropertyLogBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iPropertyLogService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除财物日志
     */
    @ApiOperation("删除财物日志")
    @SaCheckPermission("oms:propertyLog:remove")
    @Log(title = "财物日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iPropertyLogService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
